# -*- coding:utf-8 -*-
# @FileName :address_model.py
# @Date: 2024/8/3
# @Author:天空之城
from address_normalizer.app.config import TOKENIZE_MODEL
from address_normalizer.app.models.pos_tag import PosTag
from address_normalizer.app.schemas.address_schemas import AddressSchemas


class AddressModel(PosTag):
    def __init__(self, mode=TOKENIZE_MODEL):
        """
        :param mode:
        目前可选模式有rule、jieba
        """
        super().__init__()
        self.mode = mode

    def predict(self, address):
        if self.mode == 'rule':
            return self.rule_tagger(address)
        elif self.mode == 'jieba':
            return self.jieba_tagger(address)
        assert f"{self.mode} 不存在！目前仅能支持rule、jieba"

    @staticmethod
    def standard_address_process(predicted_result: AddressSchemas):
        """
        生成标准化地址。如果某个属性为 None，则将其替换为空字符串。
        :param predicted_result: 一个包含地址属性的对象
        :return: 标准化后的地址字符串
        """
        province = predicted_result.province if predicted_result.province is not None else ''
        city = predicted_result.city if predicted_result.city is not None else ''
        district = predicted_result.district if predicted_result.district is not None else ''
        other_address = predicted_result.other_address if predicted_result.other_address is not None else ''
        standard_address = f"{province}{city}{district}{other_address}"
        # 处理直辖市的重复名称问题
        standard_address = standard_address.replace('上海市上海市', '上海市')
        standard_address = standard_address.replace('北京市北京市', '北京市')
        standard_address = standard_address.replace('天津市天津市', '天津市')
        standard_address = standard_address.replace('重庆市重庆市', '重庆市')
        return standard_address

# if __name__ == '__main__':
#     address = AddressModel()
#     print(address.predict('福建省厦门市湖里区'))
